package com.xiaoq.sites.endpoint;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.sites.entity.SitesPointArEntity;
import com.xiaoq.sites.service.ISitesPointArService;
import com.xiaoq.sites.vo.SitesPointArVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/api/v1/pointar")
public class SitesPointArEndpoint {
	
	@Autowired
	private ISitesPointArService sitesPointArSer;
	
	@Autowired
	private IdGenerator idGenerator;
	
	private static final Logger LOG = LoggerFactory.getLogger(SitesPointArEndpoint.class);
	
	private class SitesPointArListResponse extends GenericListResponse<SitesPointArVo>{}
	
	@ApiOperation(value="景区Ar图片列表", notes="景区Ar图片列表")
	@ApiResponses({
		@ApiResponse(code = 200, response = SitesPointArListResponse.class, message = "景区Ar图片列表")
	})
	@GetMapping(value="/{sitesId}", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	public ResponseEntity<?> pointArImgList(@PathVariable(value="sitesId",required=true)@ApiParam(value="sitesId",required=true)String sitesId){
		List<SitesPointArEntity> sitesPointArList = sitesPointArSer.findPointArList(sitesId);
		List<SitesPointArVo> sitesPointArVoList = new ArrayList<SitesPointArVo>(); 
		SitesPointArVo sitesPointArVo = null;
		if(sitesPointArList!=null){
			for(SitesPointArEntity sitesPointAr:sitesPointArList){
				sitesPointArVo = new SitesPointArVo();
				BeanUtils.copyProperties(sitesPointAr, sitesPointArVo);
				sitesPointArVoList.add(sitesPointArVo);
			}
			
		}else{
			LOG.error("景区没有对应的AR图片！");
		}
		return SitesPointArListResponse.listNoCount(sitesPointArVoList);
	}
	
	@ApiOperation(value="对接运营后端（添加AR景点信息）", notes="对接运营后端（添加AR景点信息）")
	@ApiResponses({
		@ApiResponse(code = 200, response = SitesPointArListResponse.class, message = "对接运营后端（添加AR景点信息）")
	})
	@PostMapping(value="", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	public ResponseEntity<?> addArPoint(@RequestBody SitesPointArVo sitesPointArVo){
		SitesPointArEntity sitesPointAr = new SitesPointArEntity();
		BeanUtils.copyProperties(sitesPointArVo,sitesPointAr);
		sitesPointAr.setId(idGenerator.nextId());
		sitesPointArSer.addPointAr(sitesPointAr);
		return GenericResponse.ok();
		
	}
	
	@ApiOperation(value="对接运营后端（删除AR景点信息）", notes="对接运营后端（删除AR景点信息）")
	@ApiResponses({
		@ApiResponse(code = 200, response = SitesPointArListResponse.class, message = "对接运营后端（删除AR景点信息）")
	})
	@DeleteMapping(value="", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	public ResponseEntity<?> deleteArPoint(@RequestParam(value="id",required=true)@ApiParam(value="AR景点Id",required=true)String id){
		sitesPointArSer.deletePointAr(id);
		return GenericResponse.ok();
		
	}
	
	@ApiOperation(value="对接运营后端（修改AR景点信息）", notes="对接运营后端（修改AR景点信息）")
	@ApiResponses({
		@ApiResponse(code = 200, response = SitesPointArListResponse.class, message = "对接运营后端（修改AR景点信息）")
	})
	@PutMapping(value="", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	public ResponseEntity<?> deleteArPoint(@RequestBody SitesPointArVo sitesPointArVo){
		String id = sitesPointArVo.getId();
		SitesPointArEntity sitesPointAr = sitesPointArSer.findById(id);
		String sitesId = sitesPointArVo.getSitesId();
		if(StringUtils.isNotBlank(sitesId)){
			sitesPointAr.setSitesId(sitesId);
		}
		String pointId = sitesPointArVo.getPointId();
		if(StringUtils.isNotBlank(pointId)){
			sitesPointAr.setPointId(pointId);
		}
		
		String pointInfo = sitesPointAr.getPointId();
		if(StringUtils.isNotBlank(pointInfo)){
			sitesPointAr.setPointInfo(pointInfo);
		}
		
		Double longitude = sitesPointAr.getLongitude();
		if(longitude!=null&&longitude!=0){
			sitesPointAr.setLongitude(longitude);
		}
		Double latitude = sitesPointAr.getLatitude();
		if(latitude!=null&&latitude!=0){
			sitesPointAr.setLatitude(latitude);
		}
		List imgUrls = sitesPointAr.getImgUrls();
		if(imgUrls!=null&&imgUrls.size()!=0){
			sitesPointAr.setImgUrls(imgUrls);
		}
		
		String pointName = sitesPointAr.getPointInfo();
		if(StringUtils.isNotBlank(pointName)){
			sitesPointAr.setPointName(pointName);
		}
		sitesPointArSer.addPointAr(sitesPointAr);
		return GenericResponse.ok();
		
	}

}
